package com.zerui.manager.icc.strategy.command;

import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.dahuatech.icc.exception.ClientException;
import com.zerui.manager.icc.service.DeviceInfoService;
import com.zerui.manager.web.AccessAuthDeviceService;
import com.zerui.manager.web.enums.DeviceFromEnum;
import com.zerui.manager.web.vo.AccessAuthDeviceVO;
import com.zerui.manager.web.vo.icc.DeviceInfoVO;
import com.zerui.manager.web.vo.icc.UnitVO;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public abstract class CommandBase {

    @Resource
    private DeviceInfoService deviceInfoService;
    @Resource
    private AccessAuthDeviceService accessAuthDeviceService;


    /**
     * 读取指定设备的通道信息
     *
     * @return
     */
    public Set<String> getChannelCodes(Set<Long> deviceIds) {
        Set<String> channelCodesSet = new HashSet<>();
        try {
            List<AccessAuthDeviceVO> authDeviceVOS = accessAuthDeviceService.selectByDeviceIds(deviceIds);// 获取授权的门禁设备
            for (AccessAuthDeviceVO vo : authDeviceVOS) {
                if (vo.getDeviceFrom() != DeviceFromEnum.FROM_ICC.getCode()) continue;
                if (StringUtils.isBlank(vo.getDeviceCode())) continue;
                DeviceInfoVO iccDeviceInfo = deviceInfoService.selectByDeviceCode(vo.getDeviceCode());
                for (UnitVO uint : iccDeviceInfo.getUnits()) {
                    if (7 == uint.getUnitType()) // 单位类型为 7 才能下发门禁
                        uint.getChannels().stream().filter(c -> StringUtils.isNotBlank(c.getChannelCode())).forEach(c -> channelCodesSet.add(c.getChannelCode()));
                }
            }
            return channelCodesSet;
        } catch (ClientException e) {
            throw new BizException("查询ICC设备信息出错", e);
        }
    }

}
